//
//  TravellingViewController.m
//  Travelling Together
//
//  Created by lanou3g on 15/11/26.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "TravellingViewController.h"
#import "TravellingCell.h"
#import "DownLoad.h"
#import "Single.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"
#import "SearchViewController.h"

@interface TravellingViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong)UISegmentedControl *segmentedControl;

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic, strong)UIScrollView * sv;


@end

@implementation TravellingViewController

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.sv.frame = CGRectMake(5, 104, 750, self.tableView.frame.size.height);
    self.sv.contentSize = CGSizeMake(375, 667);
    self.sv.backgroundColor = [UIColor whiteColor];
    
    NSArray * segmenteArray = [[NSArray alloc] initWithObjects:@"游记", @"专题", nil];
    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:segmenteArray];
    self.segmentedControl.frame = CGRectMake(5, 69, 360, 30);
    self.segmentedControl.selectedSegmentIndex = 2;
    self.segmentedControl.tintColor = [UIColor colorWithRed:16/255.0 green:135/255.0 blue:217/255.0 alpha:1.0];
    
    [self.segmentedControl addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
    
    [self.view addSubview:self.segmentedControl];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 105, 360, 575) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    
    self.navigationItem.title = @"同游记";
    
    //set NavigationBar 背景颜色&title 颜色
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:16/255.0 green:135/255.0 blue:217/255.0 alpha:1.0]];
    
    CGRect rect = CGRectMake(0, 0, 188, 44);
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.backgroundColor = [UIColor clearColor];
    label.text = @"同游记";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont fontWithName:@"System Italic" size:18.0];
    self.navigationItem.titleView = label;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"SettingBarButton@2x"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftBarAction:)];
    
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"NavBar_Search_Normal@2x"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBarAction:)];
    
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[TravellingCell class] forCellReuseIdentifier:@"cell"];
    
    
    
    
    [DownLoad downLoadWithURL:@"http://chanyouji.com/api/trips/featured.json?page=1" method:@"GET" param:nil passValue:^(id data) {
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
        for (NSDictionary * dic in array) {
            Single * s = [[Single alloc] init];
            [s setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:s];
            
            NSLog(@"%@",self.dataArray);
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    }];
}
- (void)segAction:(UISegmentedControl *)sender
{
    NSInteger index = [self.segmentedControl selectedSegmentIndex];
    switch (index) {
        case 0:
            [self.tableView reloadData];
            break;
        case 1:
            [self.tableView reloadData];
            break;
        default:
            break;
    }
}


- (void)leftBarAction:(UIBarButtonItem *)sender
{
    
    
    
}

- (void)rightBarAction:(UIBarButtonItem *)sender
{
    SearchViewController * SVC  = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:SVC animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravellingCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    // 创建一个实例对象去接收数组里的数据
    Single * s = self.dataArray[indexPath.row];
    
            cell.titleLabel.text = s.name;
            cell.titleLabel.textColor = [UIColor grayColor];
            cell.dateLabel.font = [UIFont systemFontOfSize:12];
            NSArray *arr = [s.start_date componentsSeparatedByString:@"-"];
            cell.dateLabel.text = [NSString stringWithFormat:@"%@.%@.%@/%@天/%@图",arr[0], arr[1], arr[2],s.days,s.photos_count];
            cell.dateLabel.textColor = [UIColor whiteColor];
            
            [cell.imgView sd_setImageWithURL:[NSURL URLWithString:s.front_cover_photo_url] placeholderImage:[UIImage imageNamed:@"dlam.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                s.firstImage = image;
            }];
 
    
  
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 220;
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController * DVC = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:DVC animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
